package org.inspursc.s0517.health.common.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.inspursc.s0517.health.common.converter.UserConverter2SysUserDTO;
import org.inspursc.s0517.health.common.dao.SysRoleDao;
import org.inspursc.s0517.health.common.dao.SysRoleUserDao;
import org.inspursc.s0517.health.common.dao.SysUserDao;
import org.inspursc.s0517.health.common.dto.SysUserDTO;
import org.inspursc.s0517.health.common.entity.SysRole;
import org.inspursc.s0517.health.common.entity.SysRoleUser;
import org.inspursc.s0517.health.common.entity.SysUser;
import org.inspursc.s0517.health.common.service.SysUserService;
import org.inspursc.s0517.health.common.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao userDao;

    @Autowired
    private SysRoleDao roleDao;

    @Autowired
    private SysRoleUserDao roleUserDao;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    @Transactional
    public SysUserDTO save(SysUserDTO userDTO, List<Integer> roleIds) {
        SysUser user = UserConverter2SysUserDTO.converter(userDTO);
        if (StringUtils.isNotEmpty(user.getPassword()) && null == userDTO.getId()) {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        } else {
            SysUser sysUser = userDao.getOne(userDTO.getId());
            user.setPassword(sysUser.getPassword());
        }
        SysUser sysUser = userDao.save(user);
        if (null != userDTO.getId()) {
            roleUserDao.deleteBySysUserId(userDTO.getId());
        }

        String s=userDTO.getUserType().getCode();
        SysRoleUser sysRoleUser = new SysRoleUser();
        if( s=="ADMIN"){
            sysRoleUser.setSysRoleId(1);
        }else if(s.equals("APPRAISER")){
            sysRoleUser.setSysRoleId(3);
        }else if(s.equals("ORGADMIN")){
            sysRoleUser.setSysRoleId(2);
        }else{
            sysRoleUser.setSysRoleId(3);
        }
        sysRoleUser.setSysUserId(sysUser.getId());
        roleUserDao.save(sysRoleUser);
        return userDTO;
    }

    @Override
    public SysUserVO findById(Integer id) {
        SysUser sysUser = userDao.getOne(id);
        List<SysRole> roleList = roleDao.findByUserId(id);
        SysUserVO sysUserVO = UserConverter2SysUserDTO.converter2VO(sysUser);
        sysUserVO.setRoleIds(roleList);
        return sysUserVO;
    }

    @Override
    public SysUser findByUId(Integer id) {
        return userDao.findById(id).get();
    }

    @Override
    @Transactional
    public boolean deleteById(Integer id) {
        try {
            userDao.deleteById(id);
            roleUserDao.deleteBySysUserId(id);
            return true;
        } catch (Exception e) {
            log.error("【删除失败】", e.getMessage());
            return false;
        }
    }

    @Override
    public List<SysUserVO> findAll() {
        return UserConverter2SysUserDTO.converter2VO(userDao.findAll());
    }

    @Override
    public Page<SysUser> findByUserNameLike(String username, Pageable pageable) {
        if (StringUtils.isNotEmpty(username)) {
            return userDao.findByUsernameLike("%" + username + "%", pageable);
        }
        return userDao.findAll(pageable);
    }

    @Override
    public SysUserVO findByUserName(String username) {
        SysUser sysUser=userDao.findByUsername(username);
        if(null!=sysUser){
            return UserConverter2SysUserDTO.converter2VO(sysUser);
        }
        return null;
    }



    @Override
    public List<SysUserVO> findByRoleId(Integer roleId) {
        List<SysRoleUser> sysRoleUserList = roleUserDao.findBySysRoleId(roleId);
        List<SysUser> sysUserList = sysRoleUserList.stream().map(e -> userDao.getOne(e.getSysUserId())).collect(Collectors.toList());
        List<SysUserVO> sysUserVOList = UserConverter2SysUserDTO.converter2VO(sysUserList);
        return sysUserVOList;
    }
}
